var url = 'ws://192.168.0.105:8502';

var websocket = new WebSocket(url);
//实例化对象的onopen事件
websocket.onopen = function (evt) {
    console.log("conected-swoole-success");
}
//实例化对象的onmessage事件
websocket.onmessage = function (evt) {
    //evt.data
    //todo
    chart_push(evt.data);
    console.log("ws-server-return-data:" + evt.data);
}
//实例化对象的onclose事件
websocket.onclose = function (evt) {
    console.log("close");
}
//实例化对象的onerror事件
websocket.onerror = function (evt, e) {
    console.log("error:" + evt.data);
}

//把数据组装成html格式
function chart_push(data) {

    data = JSON.parse(data);
    html = '<div class="comment">';
    html += '<span> ' + data.user + ' </span>';
    html += '<span> ' + data.content + ' </span>';
    html += '</div>';

    $('#comments').prepend(html);
}